package com.huawei.aom.demo;

import com.huaweicloud.sdk.aom.v2.AomClient;
import com.huaweicloud.sdk.aom.v2.model.EventList;
import com.huaweicloud.sdk.aom.v2.model.EventModel;
import com.huaweicloud.sdk.aom.v2.model.PushEventsRequest;
import com.huaweicloud.sdk.aom.v2.model.PushEventsResponse;
import com.huaweicloud.sdk.aom.v2.region.AomRegion;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PushEventsSolution {
    private static final Logger LOGGER = LoggerFactory.getLogger(PushEventsSolution.class);

    public static void main(String[] args) {
        // 事件或者告警产生的时间，CST毫秒级时间戳
        long startsAt = System.currentTimeMillis();
        // 事件或者告警清除的时间，CST毫秒级时间戳，为0时表示未删除
        long endsAt = 0L;
        // 告警自动清除时间。毫秒数，例如一分钟则填写为60000。默认清除时间为3天,对应数字为 4320 * 1000（即：3天 * 24小时 * 60分钟 * 1000毫秒）
        long timeout = 60000L;
        // 事件或者告警附加字段 可为空
        Map<String, String> annotations = new HashMap<>();
        // 事件或者告警预留字段 为空
        Map<String, String> attachRule = new HashMap<>();
        // 事件或告警详情
        List<EventModel> events = new ArrayList<>();
        // 事件或者告警id，系统会自动生成，上报无须填写该字段
        events.add(new EventModel().withId("id")
            .withStartsAt(startsAt)
            .withEndsAt(endsAt)
            .withTimeout(timeout)
            .withMetadata(getMetaData("cpu使用率超过阈值", "Major", "alarm", "A服务", "主机", "B"))
            .withAnnotations(annotations)
            .withAttachRule(attachRule));
        EventList body = new EventList();
        body.withEvents(events);
        PushEventsRequest request = new PushEventsRequest();
        // 接口请求动作。action=clear代表清除告警,不传或者传其他值默认为上报动作
        request.withAction(PushEventsRequest.ActionEnum.fromValue(""));

        request.withBody(body);
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";
        String projectId = "<YOUR PROJECT_ID>";
        ICredential auth = new BasicCredentials().withAk(ak).withSk(sk).withProjectId(projectId);
        AomClient client = AomClient.newBuilder().withCredential(auth).withRegion(AomRegion.CN_NORTH_4).build();
        try {
            PushEventsResponse response = client.pushEvents(request);
            LOGGER.info(response.toString());
        } catch (ConnectionException | ServiceResponseException | RequestTimeoutException e) {
            LOGGER.error(e.toString());
        }
    }

    /**
     * 拼接告警或事件信息
     * @param eventName 事件或者告警名称
     * @param eventSeverity 事件级别枚举值。四种类型 "Critical", "Major", "Minor", "Info"
     * @param eventType 事件类别枚举值。event为普通告警，alarm为告警事件
     * @param resourceProvider 事件对应云服务名称
     * @param resourceType 事件对应资源类型
     * @param resourceId 事件对应资源信息
     * @return 告警或事件详情
     */
    public static Map<String, String> getMetaData(String eventName, String eventSeverity, String eventType,
        String resourceProvider, String resourceType, String resourceId) {
        Map<String, String> metadata = new HashMap<>();
        metadata.put("event_name", eventName);
        metadata.put("event_severity", eventSeverity);
        metadata.put("event_type", eventType);
        metadata.put("resource_provider", resourceProvider);
        metadata.put("resource_type", resourceType);
        metadata.put("resource_id", resourceId);
        return metadata;
    }
}